package org.xtext.telcodev.dsl.generator;




import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class XMLfile {


	
		public static Config readConfig(String uri){
			//get the factory
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			
			try {
				
				//Using factory get an instance of document builder
				DocumentBuilder db = dbf.newDocumentBuilder();
				
				//parse using builder to get DOM representation of the XML file
				Document dom = db.parse(uri);
			
				Element docEle = dom.getDocumentElement();

				
				String language = getTextValue(docEle,"language");
				String languageGenerated = getTextValue(docEle,"languageGenerated");
				String voice = getTextValue(docEle, "voice");
				
				
				
			
				Config conf= new Config(language, languageGenerated,"","", voice);
				System.out.println(conf.toString());
				return conf;
			
				
			}catch(ParserConfigurationException pce) {
				pce.printStackTrace();
			}catch(SAXException se) {
				se.printStackTrace();
			}catch(IOException ioe) {
				ioe.printStackTrace();
			}
			return null;
		}
		
		
	


	private static String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}

		




	

		


	}